package com.ddtou.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.ddtou.DbUtil;
import com.ddtou.entity.DataDownloadEntity;

public class DataEntityService {

	private DbUtil db;
	public DataEntityService() {
		this.db = new DbUtil();
	}
	
	public List<Object> getList() throws SQLException{
		List<Object> list = new ArrayList<Object>();
		String sql  = "SELECT ST_AsEWKT(geom) FROM alldata;";
		ResultSet rs = db.selectSQL(sql);
		String str ;
		try {
			while(rs.next()){
				str =rs.getString(1);
				System.out.println(str);
				list.add(str);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	
	public List<Object> getDataListWithBbox(String bboxwkt,String type){
		List<Object> list = new ArrayList<Object>();
		
		String str ;
		try {
			String sql  = "SELECT name,type,size, ST_AsEWKT(geom),gid FROM alldata where ST_Intersects(geom,ST_PolygonFromText('"
			+bboxwkt+"')) ";
			if(!type.equals("6")){
				sql+="and type='"+type+"'";
			}
			ResultSet rs = db.selectSQL(sql);
			JSONObject obj ;
			while(rs.next()){
				obj = new JSONObject();
				str =rs.getString(3);
				obj.put("name", rs.getString(1));
				obj.put("type", rs.getString(2));
				obj.put("size", rs.getString(3));
				obj.put("geometry", rs.getString(4));
				obj.put("id", rs.getString(5));
				System.out.println(str);
				list.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	
	//记录用户下载数据的日志
	
	public void userDownloadLog(DataDownloadEntity dd){
		String sql ="INSERT INTO datadownload( userid, dataid, \"date\") VALUES ("+dd.getUserid()+", "+dd.getDataid()+",  current_timestamp);";
		db.insertSQL(sql);
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
